/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "streamspromptdialog.h"

#include <QDialog>
#include <QWidget>
#include <QList>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QRadioButton>
#include <QLabel>
#include <QPushButton>

#include <QDesktopServices>

#include <QDebug>

#include "../Types/stream.h"
#include "../Types/link.h"
#include "../utils.h"

StreamsPromptDialog :: StreamsPromptDialog(const QList<Stream>& streamsList, const QUrl& selectedSource, QWidget *parent/*=0*/)
    :QDialog(parent),streamsList(streamsList)
{
    pMainGrid = new QVBoxLayout(this);

    pStreamsBox = new QGroupBox(tr("Streams"),this);
    pStreamsLayout = new QVBoxLayout(pStreamsBox);

    for ( QList<Stream>::const_iterator itStream = streamsList.begin() ; itStream != streamsList.end() ; itStream++ )
    {
        QRadioButton* pRadioButton = new QRadioButton(itStream->name,pStreamsBox);

        if ( itStream->url == selectedSource)
        {
            pRadioButton->setChecked(true);
        }

        pRadioButton->setIcon(QIcon(Utils::getFlagPathFromName(itStream->flag)));

        streamsPButtonList.push_back(pRadioButton);
        pStreamsLayout->addWidget(pRadioButton,0,Qt::AlignHCenter);
    }

    pButtonLayout = new QHBoxLayout();
    pValidateButton = new QPushButton(tr("Ok"),this);
    pCancelButton = new QPushButton(tr("Cancel"),this);

    pMainGrid->addWidget(pStreamsBox);
    pMainGrid->addLayout(pButtonLayout);
        pButtonLayout->addWidget(pValidateButton);
        pButtonLayout->addWidget(pCancelButton);

    connect(pValidateButton, SIGNAL(clicked()),this,SLOT(accept()));
    connect(pCancelButton, SIGNAL(clicked()),this,SLOT(reject()));

    qDebug() << "StreamsPromptDialog created";
}

StreamsPromptDialog :: ~StreamsPromptDialog(void)
{
    qDebug() << "StreamsPromptDialog deleted";
}

const QUrl& StreamsPromptDialog :: getSelectedSource(void)const
{
    int counter = 0;

    qDebug() << "StreamsPromptDialog :: getSelectedSource";

    for ( QList<QRadioButton*>::const_iterator itRadioButton = this->streamsPButtonList.begin() ; itRadioButton != streamsPButtonList.end() ; itRadioButton++ , counter++ )
    {
        if ( (*itRadioButton)->isChecked() == true )
        {
            return streamsList[counter].url;
        }
    }

    // We didn't found the selected stream
    qDebug() << "\tDidn't found the checked streams!";

    // We return the first
    return streamsList[0].url;
}
